package com.egao.common.manage.company;

import com.egao.common.core.annotation.ApiPageParam;
import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.company.constants.CompanyConstants;
import com.egao.common.module.company.entity.vo.PositionFormVO;
import com.egao.common.module.company.entity.vo.PositionListVO;
import com.egao.common.module.company.entity.vo.RecruitFormVO;
import com.egao.common.module.company.entity.vo.RecruitListVO;
import com.egao.common.module.company.service.RecruitService;
import com.egao.common.module.push.entity.AbilityFormDTO;
import com.egao.common.module.push.entity.PushMsg;
import com.egao.common.module.push.service.PushMsgService;
import com.egao.common.module.student.service.StudentService;
import com.egao.common.module.system.entity.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 单位招聘管理
 * @author dl
 */
@Api(tags = "单位招聘管理")
@RestController
@RequestMapping("${api}")
public class CompanyRecruitController extends BaseController {

    @Autowired
    private RecruitService recruitService;
    @Autowired
    private PushMsgService pushMsgService;
    @Autowired
    private StudentService studentService;

    @PreAuthorize("hasAuthority('company')")
    @ApiOperation(value = "招聘信息", notes = "未审核状态升序 创建时间倒序")
    @OperLog(value = "招聘信息", desc = "分页查询招聘信息")
    @ApiPageParam
    @GetMapping("/company/recruit/page")
    public PageResult<RecruitListVO> page(HttpServletRequest request) {
        PageParam<RecruitListVO> pageParam = new PageParam<>(request);
        // 未删除招聘信息
        pageParam.put("invalid", CompanyConstants.NO_FOUND_COMPANY);
        pageParam.put("user_id", getLoginUserId());
        return new PageResult<>(recruitService.listPage(pageParam), pageParam.getTotal());
    }

    @PreAuthorize("hasAuthority('company')")
    @OperLog(value = "添加招聘信息", desc = "添加招聘信息", result = true)
    @ApiOperation(value = "添加招聘信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recruitFormVO", value = "recruitFormVO", required = true, dataType = "RecruitFormVO")
    })
    @PostMapping("/company/recruit/save")
    public JsonResult save(@Valid @RequestBody RecruitFormVO recruitFormVO) {
        recruitFormVO.setCreateBy(getLoginUserId());
        //招聘信息为单位发布
        recruitFormVO.setAddType(CompanyConstants.BUSINESS_TYPE_COMPANY);
        // 设置招聘信息为待审核
        recruitFormVO.setState(CompanyConstants.RECRUIT_STATE_DSH);
        return recruitService.saveRecruit(recruitFormVO);
    }

    @PreAuthorize("hasAuthority('company')")
    @OperLog(value = "修改招聘信息", desc = "修改招聘信息", result = true)
    @ApiOperation(value = "修改招聘信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recruitFormVO", value = "recruitFormVO", required = true, dataType = "RecruitFormVO")
    })
    @PutMapping("/company/recruit/update")
    public JsonResult update(@Valid @RequestBody RecruitFormVO recruitFormVO) {
        return recruitService.updateRecruit(recruitFormVO);
    }

    @PreAuthorize("hasAuthority('company')")
    @OperLog(value = "删除招聘信息", desc = "删除招聘信息", result = true)
    @ApiOperation(value = "删除招聘信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recruitFormVO", value = "recruitFormVO", required = true, dataType = "RecruitFormVO")
    })
    @PutMapping("/company/recruit/delete")
    public JsonResult deleteRecruit(@RequestBody RecruitFormVO recruitFormVO) {
        return recruitService.deleteRecruit(recruitFormVO);
    }

    @PreAuthorize("hasAuthority('company')")
    @ApiOperation(value = "获取招聘信息详情")
    @OperLog(value = "获取招聘信息详情", desc = "获取招聘信息详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "招聘信息id", required = true, dataType = "int")
    })
    @GetMapping("/company/recruit/detail")
    public JsonResult detail(Integer id) {
        return recruitService.detail(id);
    }

    @PreAuthorize("hasAuthority('company')")
    @ApiOperation(value = "招聘信息未过期数量")
    @OperLog(value = "招聘信息未过期数量", desc = "招聘信息未过期数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "招聘信息id", required = true, dataType = "int")
    })
    @GetMapping("/company/recruit/get-number")
    public JsonResult getNumber(HttpServletRequest request) {
        PageParam<RecruitListVO> pageParam = new PageParam<>(request);
        // 未删除招聘信息
        pageParam.put("invalid", CompanyConstants.NO_FOUND_COMPANY);
        pageParam.put("user_id", getLoginUserId());
        // 未过期
        pageParam.put("expire", "0");
        return recruitService.getRecruitNumber(pageParam);
    }

    @PreAuthorize("hasAuthority('company')")
    @ApiOperation(value = "获取职位匹配度", notes = "获取职位匹配度")
    @OperLog(value = "获取职位匹配度", desc = "获取职位匹配度")
    @ApiPageParam
    @PostMapping("/company/recruit/position")
    public JsonResult getPosition(@RequestBody AbilityFormDTO abilityFormDTO) {
        return JsonResult.ok().setData(pushMsgService.queryPositionMatchingByIds(abilityFormDTO, getLoginUserId()));
    }

    @PreAuthorize("hasAuthority('company')")
    @OperLog(value = "单位招聘管理", desc = "技能语言和证书")
    @ApiOperation(value = "技能语言和证书")
    @GetMapping("/company/recruit/dict")
    public JsonResult dictMap() {
        Map<String, List<Dict>> map = studentService.dictMap();
        List<Dict> skill = map.get("intenSkill");
        List<Dict> certificate = map.get("itenCertificate");
        for (Dict item : skill) {
            item.setDisabled(true);
        }
        for (Dict item : certificate) {
            item.setDisabled(true);
        }
        return JsonResult.ok().setData(map);
    }
}
